package com.corundumstudio.socketio;

import java.util.concurrent.TimeUnit;

import org.junit.Test;

import com.corundumstudio.socketio.listener.ConnectListener;
import com.corundumstudio.socketio.listener.DataListener;

public class JustRun {
	
	
	@Test
	public void runTest() throws InterruptedException {
		Configuration config = new Configuration();
        config.setHostname("localhost");
        config.setPort(9092);

        SocketIOServer server = new SocketIOServer(config); 

        CharteventListener listner = new CharteventListener();
        listner.setServer(server);
        //   chatevent为事件名称
        server.addEventListener("chatevent", ChatObject.class, listner); 
        //启动服务
        server.start();
        TimeUnit.SECONDS.sleep(1000);

	}

}

class ChatObject {

    private String userName;
    private String message;

    public ChatObject() {
    }

    public ChatObject(String userName, String message) {
        super();
        this.userName = userName;
        this.message = message;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

}

class CharteventListener implements DataListener<ChatObject> {

    SocketIOServer server;

    public void setServer(SocketIOServer server) {
        this.server = server;
    }
    
    public void onData(SocketIOClient client, ChatObject data,
            AckRequest ackSender) throws Exception {
        //chatevent为 事件的名称，data为发送的内容
        this.server.getBroadcastOperations().sendEvent("chatevent", data);
    }
}
